<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="AdddialogVisible = true">添加轮播图</el-button>
      <el-alert
        title="轮播图最多上传四张"
        type="warning"
        center
        show-icon
        :closable="false"
        style="margin-top:20px"
      ></el-alert>
      <el-table :data="bannerList" border stripe>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="图片">
          <template slot-scope="scope">
            <img :src="scope.row.images" class="img" />
          </template>
        </el-table-column>
        <el-table-column label="路径" prop="navigator_url"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEdit(scope.row.id)"
            ></el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="remove(scope.row.id)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加 -->
    <el-dialog title="添加轮播图" :visible.sync="AdddialogVisible" width="50%" @close="AddDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="路径" prop="navigator_url">
          <el-input v-model="addForm.navigator_url"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="sort">
          <el-input v-model="addForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <el-input v-model="addForm.images" v-if="false"></el-input>
          <el-upload
            class="avatar-uploader"
            action="http://shop.cn/api/v1.Qny/upload/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="images" :src="images" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" style="border: 1px dashed #d9d9d9;"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AdddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改轮播图"
      :visible.sync="EditdialogVisible"
      width="50%"
      @close="EditDialogClosed"
    >
      <el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="80px">
        <el-form-item label="路径" prop="navigator_url">
          <el-input v-model="editForm.navigator_url"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="sort">
          <el-input v-model="editForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <el-input v-model="editForm.images" v-if="false"></el-input>
          <el-upload
            class="avatar-uploader"
            action="http://shop.cn/api/v1.Qny/upload/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="images" :src="images" class="avatar" />
            <img v-else :src="editForm.images" class="avatar" />
            <!-- <i v-else class="el-icon-plus avatar-uploader-icon" style="border: 1px dashed #d9d9d9;"></i> -->
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="EditdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { server } from '../api/index'
export default {
  data() {
    return {
      bannerList: [],
      AdddialogVisible: false,
      EditdialogVisible: false,
      images: '',
      addForm: {
        navigator_url: '',
        sort: '',
        images: ''
      },
      editForm: {
        navigator_url: '',
        sort: '',
        images: ''
      },
      addFormRules: {
        navigator_url: [
          { required: true, message: '请输入路径', trigger: 'blur' }
        ],

        sort: [{ required: true, message: '请填写序号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getBannerList()
  },
  methods: {
    async getBannerList() {
      const { data: res } = await server.getBanner('/v1.Banner/select')
      this.bannerList = res.data
    },
    AddDialogClosed() {
      ;(this.images = ''),
        (this.addForm.images = ''),
        this.$refs.addFormRef.resetFields()
    },
    EditDialogClosed() {
      this.images = ''

      this.editForm.images == ''
    },
    handleAvatarSuccess(res, file) {
      this.images = URL.createObjectURL(file.raw)
      this.images = res.data
      this.addForm.images = this.images
      //完成后清除所有的图片
      this.editForm.images == URL.createObjectURL(file.raw)
      this.editForm.images = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type == 'image/jpeg' ||
        file.type == 'image/png' ||
        file.type == 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    add() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项')
        }
        const { data: res } = await server.addBanner(this.addForm)
        if (res.code !== 200) {
          return this.$message.error('添加失败')
        }
        ;(this.addForm.images = ''),
          (this.images = ''),
          (this.AdddialogVisible = false)
        this.getBannerList()
        this.$message.success('添加成功！')
      })
    },
    async showEdit(id) {
      const { data: res } = await server.detailBanner(id)

      if (res.code !== 200) {
        return this.$message.error('获取失败')
      }
      this.editForm = res.data
      this.EditdialogVisible = true
    },
    async edit() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        //发起修改请求
        const { data: res } = await server.editBanner(
          this.editForm.id,
          this.editForm
        )

        if (res.code != 200) {
          return this.$message.error('编辑失败！')
        }
        this.getBannerList()
        this.images = ''
        this.EditdialogVisible = false
        //提示修改成功
        this.$message.success('编辑成功！')
      })
    },
    //触发删除按钮
    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //执行删除操作
    async delete(id) {
      const { data: res } = await server.delBanner(id)

      if (res.code !== 200) {
        return this.$message.error('删除失败！')
      }
      //刷新列表
      this.getBannerList()
      this.$message.success('删除成功！')
    }
  }
}
</script>
<style scoped>
.img {
  width: 120px;
  height: 60px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>